package hapi.math;

class ImmutableVector2D {
	
	public var x(default,null):Float;
	public var y(default,null):Float;
	
	public inline static var ZERO:ImmutableVector2D = new ImmutableVector2D(0,0);
	
	public function new(x,y){
		this.x = x;
		this.y = y;
	}
	
	public function mutable():Vector2D {
		return new Vector2D(x,y);
	}
	
	public inline function clone():ImmutableVector2D {
		return new ImmutableVector2D( this.x, this.y );
	}
	
	public inline function equals( v ):Bool {
		return ( this.x == v.x && this.y == v.y );
	}
	
	public inline function plusNew( v ):ImmutableVector2D {
		return new ImmutableVector2D( this.x + v.x, this.y + v.y );
	}
	
	public inline function minusNew( v ):ImmutableVector2D {
		return new ImmutableVector2D( this.x - v.x, this.y - v.y );
	}
	
	public inline function multNew( n:Float ):ImmutableVector2D {
		return new ImmutableVector2D( this.x * n, this.y * n );
	}
	
	public inline function divNew( n:Float ):ImmutableVector2D {
		return new ImmutableVector2D( this.x / n, this.y / n );
	}
	
	public inline function dot( v ):Float {
		return this.x*v.x + this.y*v.y;
	}
	
	public inline function cross( v ):Float {
		return this.x*v.y - this.y*v.x;
	}
	
	public inline function getNormal():ImmutableVector2D {
		var l:Float = this.getLength();
		if ( l != 0 )
			return this.divNew( l );
		else
			return ZERO;
	}
	
	public inline function getNegative():ImmutableVector2D {
		return new ImmutableVector2D( -this.x, -this.y );
	}
	
	public inline function negative():Void {
		this.x = - this.x;
		this.y = - this.y;
	}
	
	public inline function distance( v ):Float {
		var dx:Float = this.x - v.x;
		var dy:Float = this.y - v.y;
		return Math.sqrt( dx*dx + dy*dy );
	}
	
	public inline function getLength():Float {
		return Math.sqrt( this.x*this.x + this.y*this.y );
	}
	
	public function toString():String {
		return "ImmutableVector2D[ "+x+", "+y+" ]";
	}
}